package com.jc.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;

/**
 * 文件操作
 *
 * @author zhifeng
 */
public final class Files extends FileUtils {

    public static long checksumCRC32(InputStream in) throws IOException {
        if (in == null) {
            throw new IllegalArgumentException("InputStream can't be null.");
        }

        CRC32 crc = new CRC32();
        try {
            IOUtils.copy(new CheckedInputStream(in, crc), new NullOutputStream());
        } finally {
            IOUtils.closeQuietly(in);
        }
        return crc.getValue();
    }

}
